package org.example.score.service.xiangxi;

import com.google.gson.Gson;
import org.example.score.mapper.WeightMapper;
import org.example.score.mapper.xiangxi.XiangXiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class XiangXiServiceImpl implements XiangXiService {


    //所有相关表格数组
    private static String[] tables = {"pd_cbjczl_xx", "pd_gccch_xx", "pd_gcctd_xx", "pd_jxsp_jxcgj_xx", "pd_jxsp_jyjxyjyggxm_xx", "pd_jxsp_pxjxqk_xx", "pd_jyhd_cjjskyqk_xx", "pd_kysp_fblwqk_xx", "pd_kysp_jskycgzhqk_xx", "pd_kysp_zlzz_xx", "pd_sxjd_jxxmjdqk_xx", "pd_sxjd_sxsjsxjdqk_xx", "pd_xxcx_czbyzp_xx", "pd_xxcx_fblwqk_xx", "pd_xxcx_jsqk_xx", "pd_xxcx_yszybs_xx", "pd_xxcx_zlzzq_xx", "pd_ylkcjs_xx", "pd_ysylzy_xx", "pd_jydh_cjcxcyxljh_xx", "pd_kysp_zckyxm_xx"};

    //指标名称和要查询的表格对应数据
    private static final TableData[] TABLEDATAARRAY = {

            new TableData(1, "pd_ysylzy_xx", "优势(一流)专业", "PdYsylzyXx"),
            new TableData(2, "pd_gccch_xx", "高层次称号", "PdGccchXx"),
            new TableData(3, "pd_gcctd_xx", "高层次团队", "PdGcctdXx"),

            new TableData(4, "pd_jxsp_jxcgj_xx", "教学水平 - 教学成果奖", "PdJxspJxcgjXx"),
            new TableData(5, "pd_jxsp_jyjxyjyggxm_xx", "教学水平 - 教育教学研究与改革项目", "PdJxspJyjxyjyggxmXx"),
            new TableData(6, "pd_jxsp_pxjxqk_xx", "教学水平 - 培训进修情况", "PdJxspPxjxqkXx"),

            new TableData(7, "pd_kysp_fblwqk_xx", "科研水平 - 发表论文情况", "PdKyspFblwqkXx"),
            new TableData(8, "pd_kysp_jskycgzhqk_xx", "科研水平 - 教师科研成果转化情况", "PdKyspJskycgzhqkXx"),
            new TableData(9, "pd_kysp_zlzz_xx", "科研水平 - 专利、专著", "PdKyspZlzzXx"),
            new TableData(10, "pd_kysp_zckyxm_xx", "科研水平 - 主持科研项目 ", "PdKyspZckyxmXx"),

            new TableData(11, "pd_sxjd_jxxmjdqk_xx", "实习实践实训基地情况 - 省级以上", "PdSxjdJxxmjdqkXx"),
            new TableData(12, "pd_sxjd_sxsjsxjdqk_xx", "实习实践实训基地情况", "PdSxjdSxsjsxjdqkXx"),

            new TableData(13, "pd_ylkcjs_xx", "一流课程建设", "PdYlkcjsXx"),
            new TableData(14, "pd_cbjczl_xx", "出版教材质量", "PdCbjczlXx"),

            new TableData(15, "pd_jydh_cjcxcyxljh_xx", "高影响力教育活动参与情况 - 参加创新创业训练计划 ", "PdJydhCjcxcyxljhXx"),
            new TableData(16, "pd_jyhd_cjjskyqk_xx", "高影响力教育活动参与情况 - 参加教师科研情况", "PdJyhdCjjskyqkXx"),

            new TableData(17, "pd_xxcx_czbyzp_xx", "学习成效 - 创作、表演的代表性作品", "PdXxcxCzbyzpXx"),
            new TableData(18, "pd_xxcx_fblwqk_xx", "学习成效 - 学生发表论文", "PdXxcxFblwqkXx"),
            new TableData(19, "pd_xxcx_jsqk_xx", "学习成效 - 学生获省级及以上各类竞赛奖励情况", "PdXxcxJsqkXx"),
            new TableData(20, "pd_xxcx_yszybs_xx", "学习成效 - 艺术类专业比赛获奖", "PdXxcxYszybsXx"),
            new TableData(21, "pd_xxcx_zlzzq_xx", "学习成效 - 学生专利（著作权）授权情况", "PdXxcxZlzzqXx")
    };

    //指标名称
    private static String[] zhibiao = {"优势（一流）专业", "高层次称号", "高层次团队", "教学水平", "科研水平", "实习实践实训基地情况", "一流课程建设", "出版教材质量", "高影响力教育活动参与情况", "学习成效"};


    //按照校内专业名称 xnzymc 字段 来查询的表格数组
    private static String[] xnzymcTables = {"pd_gccch_xx", "pd_jyhd_cjjskyqk_xx", "pd_xxcx_czbyzp_xx", "pd_xxcx_fblwqk_xx", "pd_xxcx_jsqk_xx", "pd_xxcx_yszybs_xx", "pd_xxcx_zlzzq_xx", "pd_jydh_cjcxcyxljh_xx"};


    // 通过指标名称获取获取表格数据
    public static List<TableData> getTableData(String tablename) {
        List<TableData> tableDataList = new ArrayList<>();
        for (TableData tableData : TABLEDATAARRAY) {
            if (tableData.getText().contains(tablename)) {
                tableDataList.add(tableData);
            }
        }
        return tableDataList;
    }

    boolean isXnzymc(String xnzymc) {
        for (String table : xnzymcTables) {
            if (table.contains(xnzymc)) {
                return true;
            }
        }
        return false;
    }

    @Autowired
    public XiangXiMapper xiangXiMapper;


    @Override
    public String selectXiangxi(String zhibiao, String code, String xnzymc) {
        List<TableData> ZB = getTableData(zhibiao);
        System.out.println(zhibiao + "--" + code + "--" + xnzymc + " ------------" + ZB.size());

//        List<List<Map<String, Object>>> list = new ArrayList<>();
        List<Map<String, Object>> list1 = new ArrayList<>();


        for (TableData tableData : ZB) {
            boolean local = isXnzymc(tableData.getTable());
            List<Map<String, Object>> res = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            if (local) {
                res = xiangXiMapper.selectxnzymc(tableData.getTable(), code, xnzymc);
            } else {
                res = xiangXiMapper.selectzymc(tableData.getTable(), code, xnzymc);
            }


//            list.add(res);
            List<String> columns = xiangXiMapper.getColumnNames(tableData.getTable());
            map.put("name", tableData.getText());
            map.put("entity", tableData.getEntity());
            map.put("data", res);
            map.put("columns", columns);
            list1.add(map);
        }

        System.out.println("详细------------------------------------------------------------------------------------");
        // 将List<List<Map<String, Object>>>转换为JSON字符串
        Gson gson = new Gson();
//        String json = gson.toJson(list);
        String json1 = gson.toJson(list1);

        System.out.println("详细=======================================================================================");
//        System.out.println(json);
//        System.out.println("---------------------------------------------------------------------------------------");
//        System.out.println("---------------------------------------------------------------------------------------");
        System.out.println(json1);
        System.out.println("详细=======================================================================================");

        return json1;
    }




}
